class maxmin_byperiod:
    def __init__(self,timeperiod=5):
        self.his_data=[]
        self.timeperiod=timeperiod
        self.pretimekey=None
    def oncalc(self,d,timekey=None):
        if timekey:
            if timekey==self.pretimekey:
                self.his_data[-1]=d
            else:
                self.his_data.append(d)
        else:
            self.his_data.append(d)
        while len(self.his_data)>self.timeperiod:
            self.his_data.pop(0)
        if len(self.his_data)==self.timeperiod:
            min0=min(self.his_data)
            max0=max(self.his_data)
            if d==min0:
                return "min"
            elif d==max0:
                return "max"
            else:
                return None
        else:
            return None

if __name__ == '__main__':
    from jili.core import load, save
    from research.calcor.calcors_graph import graph_calcor
    k1m = load(r"D:\data\future_k1m_tq\TA005\TA005_20200102.pkl")
    roc_stat0=maxmin_byperiod(timeperiod=5)
    cc = [{'calc_cmd': 'ta', 'cmd': 'EMA', 'out': ['ema30'], 'input': {'price': 'close'}, 'arg': {'timeperiod': 30},
           'batch': 30},
          {'calc_cmd': 'ta', 'cmd': 'EMA', 'out': ['ema10'], 'input': {'price': 'close'}, 'arg': {'timeperiod': 10},
           'batch': 10},
          {'calc_cmd': 'function', 'function': roc_stat0.oncalc, 'out': ['maxmin'], 'input': ["volume","timekey"]}]
    c = graph_calcor(cc)
    k1ms = k1m[:29]
    for t in k1ms:
        t = c.onbar(t)
    print(t)
    t1 = k1m[30]
    t1["timekey"] = t["timekey"]
    t1 = c.onbar(t1)
    print(t1)
    print("end")